import sys,time
from qasync import QEventLoop, asyncSlot
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
import asyncio

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout()
        self.button = QPushButton("运行协程")
        self.button.clicked.connect(self.run_coroutine)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)

    @asyncSlot()
    async def run_coroutine(self):
        await asyncio.sleep(2)  # 模拟异步操作
        print("协程执行完成")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    # 创建 asyncio 事件循环并绑定到 Qt
    loop = QEventLoop(app)
    asyncio.set_event_loop(loop)

    sys.exit(app.exec())